/*
 * Copyright 2021 tu.cn All right reserved. This software is the
 * confidential and proprietary information of tu.cn ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Tu.cn
 */
package com.genersoft.iot.vmp.vmanager.record;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author rox_p
 * @date 2021/9/24 16:36
 */
public class TcpService extends Thread{
    Socket clientSocket = null;
    ServerSocket serviceSocket = null;
    OutputStream outputStream = null;

    public void run() {
        try{
            long ssrc = 255;
            int send_interval_ms = 50;
            int audio_need_len = 400;
//        int localPort = 56200;
            serviceSocket = new ServerSocket(8000);

                clientSocket = serviceSocket.accept();
                System.out.println(clientSocket.getInetAddress().getHostAddress() + "SUCCESS TO CONNECT...");

                // InputStream inputStream=clientSocket.getInputStream();
                outputStream = clientSocket.getOutputStream();

                RtspPacketEncode rtspPacketEncode=new RtspPacketEncode();
                byte[] b= this.voiceToByte();
                rtspPacketEncode.h264ToRtp(b,b.length,outputStream);
//                InputStream audio_stream = new FileInputStream("E:\\szrm\\workspace\\study\\simpProject\\src\\com\\mq.g711a");
//                RtpPacket rtp = new RtpPacket(ssrc);
//                byte[] audio_data = new byte[audio_need_len];
//                int i = 0;
//                while (audio_stream.read(audio_data) == audio_need_len) {
//
//                    byte[] payload = rtp.packet_g711a(audio_data, audio_data.length);
//
//                    outputStream.write(payload);
//
//                    System.out.println(Arrays.toString(payload));
//                    System.out.println("发送" + i + "消息-----------------");
//                    i++;
//                    try {
//                        Thread.sleep(send_interval_ms);
//
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                        break;
//                    }
//                }

//                clientSocket.shutdownInput();
//                clientSocket.shutdownOutput();


//            byte[] bytes = new byte[1024];
//            inputStream.read(bytes);
//            System.out.println("---------client------------------:"+bytes);
            //读取数据

            // listenService(bytes);
        }catch (Exception ex){
            ex.printStackTrace();
        }
//        finally {
//            try {
//                outputStream.close();
//                clientSocket.close();
//
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
    }

    public static byte[] voiceToByte(){
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try{
            BufferedInputStream in = new BufferedInputStream(new FileInputStream("E:\\szrm\\workspace\\study\\simpProject\\src\\com\\mq.g711a"));
            int read;
            byte[] buff = new byte[1024];
            while ((read = in.read(buff)) > 0)
            {
                out.write(buff, 0, read);
            }
            out.flush();
        }catch (Exception e){
            e.printStackTrace();
        }
        byte[] audioBytes = out.toByteArray();
        return audioBytes;
    }





//    public void run() {
//        try{
//            long ssrc = 255;
//            int send_interval_ms = 50;
//            int audio_need_len = 400;
////        int localPort = 56200;
//            serviceSocket = new ServerSocket(8000);
//            while (true) {
//
//                clientSocket = serviceSocket.accept();
//                System.out.println(clientSocket.getInetAddress().getHostAddress() + "SUCCESS TO CONNECT...");
//
//                // InputStream inputStream=clientSocket.getInputStream();
//                outputStream = clientSocket.getOutputStream();
//
//
//                InputStream audio_stream = new FileInputStream("E:\\szrm\\workspace\\study\\simpProject\\src\\com\\mq.g711a");
//                RtpPacket rtp = new RtpPacket(ssrc);
//                byte[] audio_data = new byte[audio_need_len];
//                int i = 0;
//                while (audio_stream.read(audio_data) == audio_need_len) {
//
//                    byte[] payload = rtp.packet_g711a(audio_data, audio_data.length);
//
//                    outputStream.write(payload);
//
//                    System.out.println(Arrays.toString(payload));
//                    System.out.println("发送" + i + "消息-----------------");
//                    i++;
//                    try {
//                        Thread.sleep(send_interval_ms);
//
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                        break;
//                    }
//                }
//                outputStream.flush();
//                clientSocket.shutdownInput();
//                clientSocket.shutdownOutput();
//            }
//
////            byte[] bytes = new byte[1024];
////            inputStream.read(bytes);
////            System.out.println("---------client------------------:"+bytes);
//            //读取数据
//
//            // listenService(bytes);
//        }catch (Exception ex){
//            ex.printStackTrace();
//        }finally {
//            try {
//                outputStream.close();
//                clientSocket.close();
//
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }

}
